<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年级" prop="grade">
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入年级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学号" prop="studentNumber">
        <el-input
          v-model="queryParams.studentNumber"
          placeholder="请输入学号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宿舍号" prop="roomNumber">
        <el-input
          v-model="queryParams.roomNumber"
          placeholder="请输入宿舍号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:assignments:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:assignments:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:assignments:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assignmentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" prop="id" width="50"/>
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="年级" align="center" prop="grade" />
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column label="宿舍名" align="center" prop="hostel.roomNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:assignments:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:assignments:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加宿舍分配管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="assignmentsRef" :model="form" :rules="rules" label-width="80px">
        <!--        传递学号 、 学生姓名 、 宿舍号 、 书院名-->
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="学号" prop="studentNumber">
          <el-input v-model="form.studentNumber" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="所属书院" prop="hostelName">
<!--          改成下拉框-->
          <el-select v-model="form.hostelName" placeholder="请选择书院名称">
            <el-option v-for="item in hostelList" :key="item.id" :label="item.hostelName" :value="item.hostelName" />
          </el-select>
<!--          <el-input v-model="form.hostelName" placeholder="请输入书院名称" />-->
        </el-form-item>
        <el-form-item label="宿舍号" prop="roomNumber">
          <el-input v-model="form.roomNumber" placeholder="请输入宿舍号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

<!--    修改宿舍分配管理对话框   -->
    <el-dialog :title="title" v-model="openUpdateForm" width="500px" append-to-body>
      <el-form ref="assignmentsRef" :model="updateForm" :rules="rules" label-width="80px">
        <!--        传递学号 、 学生姓名 、 宿舍号 、 书院名-->
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="updateForm.studentName" placeholder="请输入学生姓名" disabled />
        </el-form-item>
        <el-form-item label="学号" prop="studentNumber">
          <el-input v-model="updateForm.studentNumber" placeholder="请输入学号" disabled />
        </el-form-item>
        <el-form-item label="所属书院" prop="hostelName">
<!--          <el-input v-model="updateForm.hostelName" placeholder="请输入书院名称" />-->
          <el-select v-model="updateForm.hostelName" placeholder="请选择书院名称">
            <el-option v-for="item in hostelList" :key="item.id" :label="item.hostelName" :value="item.hostelName" />
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍号" prop="roomNumber">
          <el-input v-model="updateForm.roomNumber" placeholder="请输入宿舍号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitUpdateForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Assignments">
import { listAssignments, getAssignments, delAssignments, addAssignments, updateAssignments } from "@/api/manage/assignments";

const { proxy } = getCurrentInstance();

const assignmentsList = ref([]);
const open = ref(false);
const openUpdateForm = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
// 定义书院列表
const hostelList = ref([
  { id: 1, hostelName: '四实书院' },
  { id: 2, hostelName: '双馨书院' },
  { id: 3, hostelName: '一善书院' },
  { id: 4, hostelName: '八方书院' },
  { id: 5, hostelName: '拾德书院' }
]);
const data = reactive({
  updateForm: {},
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    grade: null,
    major: null,
    studentId: null,
    hostelId: null,
  },
  rules: {
    studentName: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    studentNumber: [
      { required: true, message: "学号不能为空", trigger: "blur" }
    ],
    hostelName: [
      { required: true, message: "书院名不能为空", trigger: "blur" }
    ],
    roomNumber: [
      { required: true, message: "宿舍号不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询宿舍分配管理列表 */
function getList() {
  loading.value = true;
  listAssignments(queryParams.value).then(response => {
    assignmentsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  openUpdateForm.value = false;
  reset();
  resetUpdateForm();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    grade: null,
    major: null,
    studentId: null,
    hostelId: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("assignmentsRef");
}
// 重置更新表单
function resetUpdateForm() {
  updateForm.value = {
    id: null,
    grade: null,
    major: null,
    studentId: null,
    hostelId: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
    studentName: null,
    studentNumber: null,
    hostelName: null,
    roomNumber: null
  };
  proxy.resetForm("updateFormRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加宿舍分配管理";
}

/** 修改按钮操作 */
const updateForm = ref({});
function handleUpdate(row) {
  resetUpdateForm();
  const _id = row.id || ids.value
  getAssignments(_id).then(response => {
    updateForm.value = response.data;
    console.log(updateForm);
    openUpdateForm.value = true;
    title.value = "修改宿舍分配管理";
  });
}

/** 提交按钮 */
function submitForm() {
        addAssignments(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
}

/** 修改提交按钮 */
function submitUpdateForm() {
  updateAssignments(updateForm.value).then(response => {
    proxy.$modal.msgSuccess("修改成功");
    openUpdateForm.value = false;
    getList();
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除宿舍分配管理编号为"' + _ids + '"的数据项？').then(function() {
    return delAssignments(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/assignments/export', {
    ...queryParams.value
  }, `assignments_${new Date().getTime()}.xlsx`)
}

getList();
</script>
